package ch02_data.enums;

/**
 * 用法二：向枚举中添加新方法
 *
 * @author guod
 * @version 1.0
 */
public enum EnumMain02 {
    RED("红色", 1),
    GREEN("绿色", 2),
    BLANK("白色", 3),
    YELLOW("黄色", 4);

    // 成员变量
    private final String name;
    private final int index;

    public String getName() {
        return name;
    }

    public int getIndex() {
        return index;
    }

    EnumMain02(String name, int index) {
        this.name = name;
        this.index = index;
    }

    public static String getName(int index) {
        // 方法1：values
        for (EnumMain02 c : EnumMain02.values()) {
            if (c.getIndex() == index) {
                return c.name;
            }
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(EnumMain02.RED.getName());
        System.out.println(EnumMain02.RED.getIndex());
        System.out.println(EnumMain02.getName(4));
        // 方法2：根据提供的objName，返回对应的枚举对象
        final EnumMain02 yellow = EnumMain02.valueOf("YELLOW");
        System.out.println(yellow.name);
    }
}
